#include "tempiritornol.h"

#include "QtSerialization.h"

string TempiRitornoL::m_objname = "TempiRitornoL";
ExaFileObj TempiRitornoL::m_file_obj = EXA_FILE_OBJ_TEMPIRITORNOL;

TempiRitornoL::TempiRitornoL() :
    PKeyList(this)
{

}

TempiRitornoL::TempiRitornoL(const ExaTabRef& id_tab) :
    PKeyList(this,id_tab)
{

}

TempiRitornoL::~TempiRitornoL()
{
    m_erase();
}

const TempiRitorno& TempiRitornoL::getAt(int i) const
{
    return(m_list.at(i));
}

int TempiRitornoL::sizeOf( void ) const
{
    return(m_list.size());
}

TempiRitorno& TempiRitornoL::valueAt(int i)
{
    return(m_list[i]);
}

void  TempiRitornoL::remove( int i )
{
    removeRelatedAt(i);
    m_list.removeAt(i);
}

void  TempiRitornoL::insertAt( int i, int nb )
{
    int it;
    for ( it = 0; it < nb; it++)
    {
        TempiRitorno tr(this);
        m_list.insert(i+it,tr);
    }
}

void TempiRitornoL::reportMake( const string& objname ) const
{
    if (getReportCurrentStyle() == STYLE_VERBOSE)
    {
        int i;
        PReport::setReportStyle(STYLE_COMPACT);
        setReportOpt("N",false);
        for (i=0; i<sizeOf(); ++i)
        {
          getAt(i).reportMake();
        }
        resetReportStyle();
    }
    else if (getReportCurrentStyle() == STYLE_COMPACT)
    {

    }
    else if (getReportCurrentStyle() == STYLE_EXTENDED)
    {
        // qDebug()<<"STYLE_EXTENDED";
        int i;
        PReport::setReportStyle(STYLE_COMPACT);
        setReportOpt("N",false);
        list <PKeyItem> pkey_list;
        list <PKeyItem> :: const_iterator it;
        PKeyItem i_key;
        for (i=0; i<sizeOf(); ++i)
        {
            getAt(i).reportMake();
            pkey_list = relatedAdiacentAt(i);
            // qDebug() << "row = " << i << "pkey_list.size = " << pkey_list.size();
            for (it = pkey_list.begin(); it != pkey_list.end(); ++it)
            {
                i_key = *it;
                PReport::setReportStyle(STYLE_VERBOSE);
                i_key.reportMake();
            }
            PReport::setReportStyle(STYLE_COMPACT);
        }
        resetReportStyle();
    }
    else if (getReportCurrentStyle() == STYLE_JOURNAL)
    {
        addInLine(PKeyList::journal());
        moveLineInBlock(true);
    }
    setReportOpt("N",true);
    PReport::reportMake(m_objname);
}

bool TempiRitornoL::hasRefAt( int i, const PKeyItem& k ) const
{
    return false;
}

bool TempiRitornoL::setRefAt( int i, const PKeyItem& k )
{
    return false;
}

void TempiRitornoL::rename( int  i, const string& key, const string& descr)
{
    renameRelatedAt(i,key,descr);
    valueAt(i).setKeyStr(key);
    valueAt(i).setDescr(descr);
}

void TempiRitornoL::save ( void )
{
    m_list_cache = m_list;
}

void TempiRitornoL::restore ( void )
{
    m_list = m_list_cache;
}

ExaFileErr TempiRitornoL::fileSave (const string& file_name) const
{
    ExaFileErr file_err;
    file_err = fileControl(file_name,EXA_FILE_OM_WRITE_ONLY);

    if ( file_err == EXA_FILE_ERR_OK )
    {
        switch (fileMagic())
        {
            case EXA_MN_1_0_0_BIN_SLS:
            {
                /**************************************************/
                /*           FILE BINARIO SLS V 100               */
                /**************************************************/
                // apro intestazione dati binari
                QFile file(QSTR(file_name));
                if (file.open(QIODevice::WriteOnly))
                {
                    // salvo i dati binari
                    QDataStream out(&file);
                    out.setVersion(QDataStream::Qt_4_5);
                    out << fileMagic() << fileVersion() << *this ;
                }
                else
                {
                    return EXA_FILE_ERR_GENERIC;
                }
                /**************************************************/
            } break;
            default:
                return EXA_FILE_ERR_MN_UNKNOWN;
        }
    }
    fileReset();
    return file_err;
}

ExaFileErr TempiRitornoL::fileOpen (const string& file_name)
{
    ExaFileErr file_err;
    file_err = fileControl(file_name,EXA_FILE_OM_READ_ONLY);

    if ( file_err == EXA_FILE_ERR_OK )
    {
        switch (fileMagic())
        {
            case EXA_MN_1_0_0_BIN_SLS:
            {
                /**************************************************/
                /*           FILE BINARIO SLS V 100               */
                /**************************************************/
                // apro intestazione dati binari
                QFile file(QSTR(file_name));
                if (file.open(QIODevice::ReadOnly))
                {
                    QDataStream in(&file);
                    in.setVersion(QDataStream::Qt_4_5);
                    quint32 magic, version;
                    in >> magic >> version;

                    // elimino gli elementi della lista
                    this->removeAll();

                    // carico la lista
                    in >> *this;
                }
                else
                {
                    return EXA_FILE_ERR_GENERIC;
                }
                /**************************************************/
            } break;
            default:
                return EXA_FILE_ERR_MN_UNKNOWN;
        }
    }
    fileReset();
    return file_err;
}

ExaFileObj TempiRitornoL::fileObj ( void ) const
{
    return m_file_obj;
}

